import java.util.Arrays;
/**1
 * Created with IntelliJ IDEA.
 * Description:
 * User: happyiu
 * Date: 2021-11-06
 * Time: 9:22
 */
public class TestDemo {

    public static void main1(String[] args) {

        int [] array1 = {1,2,3,4,5};
        int [] array2 = new int[5];
        int [] array3 = new int[]{1,2,3,4,5};
        // 获取数组长度
        System.out.println("length: " + array1.length); // 执行结果: 5
        // 访问数组中的元素
        System.out.println(array1[0]); // 执行结果: 1
        System.out.println(array1[1]); // 执行结果: 2
        array2[0] = 100;
        array2[0] = 10;
        System.out.println(array2[0]); // 执行结果: 100
    }


    public static void main2(String[] args) {

        int[] arr = {1, 2, 3, 4, 5};
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

    }


    public static void main3(String[] args) {
        int[] arr = {1, 2, 3};
        for (int x : arr) {
            System.out.println(x);
        }

    }
    public static void printArray2(int[] a) {
        for (int x : a) {
            System.out.println(x);
        }
    }

    public static void main4(String[] args) {
        int[] arr = {1, 2, 3};
        printArray(arr);
    }

    public static void main5(String[] args) {
        int[] arr = {1, 2, 3};
        func(arr);
        System.out.println("arr[0] = " + arr[0]);
    }
    public static void func(int[] a) {
        a[0] = 10;
        System.out.println("a[0] = " + a[0]);
    }
//    执行结果
//    a[0] = 10
//    arr[0] = 10

    public static void print(int [] a){
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]);
        }
        System.out.println();
    }

    public static void main6(String[] args) {
        int[] arr = new int[]{1, 2, 3};
        print(arr);
    }

    public static void main7(String[] args) {
        int[] arr = null;
        System.out.println(arr[0]);
//        执行结果 :空错误
//        Exception in thread "main" java.lang.NullPointerException
//        at Test.main(Test.java:6)
    }
   /* // 直接修改原数组
        public static void main8(String[] args) {
            int[] arr = {1, 2, 3};
            transform(arr);
            printArray(arr);
        }
        public static void printArray(int[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
        public static void transform(int[] arr) {
            for (int i = 0; i < arr.length; i++) {
                arr[i] = arr[i] * 2;
            }
        }*/

    // 返回一个新的数组
        public static void main9(String[] args) {
            int[] arr = {1, 2, 3};
            int[] output = transform(arr);
            printArray(output);
        }
        public static void printArray(int[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
        public static int[] transform(int[] arr) {
            int[] ret = new int[arr.length];
            for (int i = 0; i < arr.length; i++) {
                ret[i] = arr[i] * 2;
            }
            return ret;
        }


    public static void main10(String[] args) {

        int[][] arr= new int[][]{{1,2,3},{4,5,6}};
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.printf("%d\t", arr[i][j]);
            }
            System.out.println("");
        }

    }


/*    public static void main(String[] args) {

        int[] arr = {1,2,3,4,5,6};
        String newArr = Arrays.toString(arr);
        System.out.println(newArr);
        // 执行结果
        //[1, 2, 3, 4, 5, 6]
    }*/

    public static void main11(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(toString(arr));
    }
    public static String toString(int[] arr) {
        String ret = "[";
        for (int i = 0; i < arr.length; i++) {
// 借助 String += 进行拼接字符串
            ret += arr[i];
// 除了最后一个元素之外, 其他元素后面都要加上 ", "
            if (i != arr.length - 1) {
                ret += ", ";
            }
        }
        ret += "]";
        return ret;
    }


    public static void main12(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        int[] newArr = Arrays.copyOf(arr, arr.length);
        System.out.println("newArr: " + Arrays.toString(newArr));
        arr[0] = 10;
        System.out.println("arr: " + Arrays.toString(arr));
        System.out.println("newArr: " + Arrays.toString(newArr));
         // 拷贝某个范围.
//        int[] newArr = Arrays.copyOfRange(arr, 2, 4);
//        System.out.println("newArr2: " + Arrays.toString(newArr2));
    }

    public static void main13(String[] args) {

        int[] arr = {1,2,3,4,5,6};
        int[] newArr = arr;
        //警告：局部变量' newArr '是多余
        System.out.println("arr: " + Arrays.toString(arr));
        System.out.println("newArr2: " + Arrays.toString(newArr));

        arr[0]=5;
        System.out.println("arr: " + Arrays.toString(arr));
        System.out.println("newArr2: " + Arrays.toString(newArr));

    }

    public static int[] copyOf(int[] arr) {
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i];
        }
        return ret;
    }

    public static void main14(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        int[] newArr =copyOf(arr);
        System.out.println("arr: " + Arrays.toString(arr));
        System.out.println("newArr2: " + Arrays.toString(newArr));
    }

    public static void main15(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(max(arr));
    }
    public static int max(int[] arr) {
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        return max;
    }

    public static void main16(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(avg(arr));
    }
    public static double avg(int[] arr) {
        int sum = 0;
        for (int x : arr) {
            sum += x;
        }
        return (double)sum / (double)arr.length;
    }

    public static int find(int[] arr, int toFind) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == toFind) {
                return i;
            }
        }
        return -1; // 表示没有找到
    }

    public static void main17(String[] args) {
        int[] arr = {1,2,3,10,5,6};
        System.out.println(find(arr, 10));
    }


    public static int binarySearch(int[] arr, int toFind) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (toFind < arr[mid]) {
// 去左侧区间找
                right = mid - 1;
            } else if (toFind > arr[mid]) {
// 去右侧区间找
                left = mid + 1;
            } else {
// 相等, 说明找到了
                return mid;
            }
        }
// 循环结束, 说明没找到
        return -1;
    }

    public static void main18(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(binarySearch(arr, 6));
    }

    public static boolean isSorted(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                return false;
            }
        }
        return true;
    }

    public static void main20(String[] args) {
        int[] arr = {1,2,3,10,5,6};
        System.out.println(isSorted(arr));
    }

    public static void bubbleSort(int[] arr) {
        for (int bound = 0; bound < arr.length; bound++) {
            for (int cur = arr.length - 1; cur > bound; cur--) {
                if (arr[cur - 1] > arr[cur]) {
                    int tmp = arr[cur - 1];
                    arr[cur - 1] = arr[cur];
                    arr[cur] = tmp;
                }
            }
        }
    }

    public static void main21(String[] args) {
        int[] arr = {9, 5, 2, 7};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void reverse(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }

    public static void main22(String[] args) {
        int[] arr = {1, 2, 3, 4};
        reverse(arr);
        System.out.println(Arrays.toString(arr));
    }



}




